﻿using System;
using System.Xml;
using System.IO;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace FlexControls
{
    public struct flexRadioButton
    {
        public RadioButton fcRadio;
        public string strTag;

    }
    public struct flexTextBox
    {
        public fcTextBox fcTextBox;

        //public 
    }
    public struct flexButton
    {
        public fcButton fcButton;
        public string strTag;
        public string strProc_Add;
        public string strProc_Delete;
        public string strProc_Update;
        public string strProc_Search;
        public int intState;

        public int intIDForm;
    }
    public struct flexForm
    {
        //Số của form
        public int intIDForm;
        public string strProc;
        public Form fcForm;
        //Đã hoặc chưa đc khởi tạo
        public bool booActive;
        public bool booIsShow;
        public string strFormTable;
        //procedure load dữ liệu cho listview
        public string strTitle;
        public string strProc_ListView;
        public string strProc_SaveData;
        public string strProc_tags;
        //List view
        public int[] intListViewId;
        public int intSoListViewInForm;
        //Button
        public int[] intButtonID;
        public int intSoButtonInForm;
        //TextBox
        public int[] intTextBoxID;
        public int intSoTextBoxInForm;
        //RadioButton
        public int[] intRadioID;
        public int intSoRadioInForm;
    }

    public struct flexController
    {
        public flexForm[] fcForms;
        public flexListView[] fcListView;
        public flexButton[] fcButton;
        public flexTextBox[] fcTextBox;
        public flexRadioButton[] fcRadio;
        public int intIdListView;
        public int intIDButton;
        public int intIDTextBox;
        public int intIDRadioButton;
        public int intSoForms;
        


    }
    public struct flexListView
    {
        public fcListView fcListView;
        public string strTag;
        public int intIDListView;
        
        public string strProc;
    }
    public class flexControls
    {
        //SQL
        public SqlConnection fcSQLConn ;
 
        //Button
        public const int STATE_THOAT = 0;
        public const int STATE_ADD = 1;
        public const int STATE_DELETE = 3;
        public const int STATE_UPDATE = 2;
        public const int STATE_SEARCH = 4;
        public const int STATE_CLEAR = 10;
        //End Button
        //Hằng số khai báo mảng Controller
        public const int MAX_FORMS = 50;
        public const int MAX_LISTVIEW = 100;
        public const int MAX_BUTTON = 50;
        public const int MAX_TEXTBOX = 100;
        public const int MAX_RADIO = 50;
        //Hằng số cho database xml
        public const string XMLFILENAME = "DatabaseInfo";
        public const int MAXELEMENT = 10;
        public const byte ELE_SERVER = 0;
        public const byte ELE_USER = 1;
        public const byte ELE_PASSWORD = 2;
        public const byte ELE_DATABASE = 3;
        public const string SERVER = "Server";
        public const string USER = "User";
        public const string PASSWORD = "Password";
        public const string DATABASE = "database";
        public string[] strSQL;
        public string strServer;
        public string strUser;
        public string strPassword;
        public string strDatabase;
        //Form Database Connection
        public fcConnection fcConnect = new fcConnection();
        //Database ADODB Connection
        public ADODB.Connection fcConn;
        public flexController Forms_QLHS;
        public flexControls()

        {
            InitializeComponent();
        }
        private void InitializeComponent()
        {
            Forms_QLHS = new flexController();
            Forms_QLHS.fcForms = new flexForm[MAX_FORMS];
            Forms_QLHS.intIdListView = 0;
            for (int i = 0; i < MAX_FORMS; i++)
            {
                Forms_QLHS.fcForms[i] = new flexForm();
                Forms_QLHS.fcForms[i].strProc = "";
                Forms_QLHS.fcForms[i].intIDForm = 0;
                Forms_QLHS.fcForms[i].booActive = false;
                Forms_QLHS.fcForms[i].booIsShow = false;
                Forms_QLHS.fcForms[i].strFormTable = "";
                Forms_QLHS.fcForms[i].strProc_ListView = "";
                Forms_QLHS.fcForms[i].strProc_SaveData = "";
                //listview
                Forms_QLHS.fcForms[i].intListViewId = new int[MAX_LISTVIEW];
                Forms_QLHS.fcForms[i].intSoListViewInForm = 0;
                //button
                Forms_QLHS.fcForms[i].intButtonID = new int[MAX_BUTTON];
                Forms_QLHS.fcForms[i].intSoButtonInForm = 0;
                //textbox
                Forms_QLHS.fcForms[i].intTextBoxID = new int[MAX_TEXTBOX];
                Forms_QLHS.fcForms[i].intSoTextBoxInForm = 0;
                Forms_QLHS.fcForms[i].intRadioID = new int[MAX_RADIO];
                Forms_QLHS.fcForms[i].intSoRadioInForm = 0;
            }
            //List view controller
            Forms_QLHS.fcListView = new flexListView[MAX_LISTVIEW];
            for (int i = 0; i < MAX_LISTVIEW; i++)
            {
                Forms_QLHS.fcListView[i] = new flexListView();
                Forms_QLHS.fcListView[i].intIDListView = 0;
                Forms_QLHS.fcListView[i].strProc = "";
                Forms_QLHS.fcListView[i].strTag = "";
            }
            //Button Controller
            Forms_QLHS.fcButton = new flexButton[MAX_BUTTON];
            for (int i = 0; i < MAX_BUTTON; i++)
            {
                Forms_QLHS.fcButton[i] = new flexButton();
                Forms_QLHS.fcButton[i].strTag = "";
                Forms_QLHS.fcButton[i].intIDForm = 0;
            }
            //Textbox Controller
            Forms_QLHS.fcTextBox = new flexTextBox[MAX_TEXTBOX];
            for (int i = 0; i < MAX_TEXTBOX; i++)
            {
                Forms_QLHS.fcTextBox[i] = new flexTextBox();
            }
            //Radio 
            Forms_QLHS.fcRadio = new flexRadioButton[MAX_RADIO];
            for (int i = 0; i < MAX_RADIO; i++)
            {
                Forms_QLHS.fcRadio[i] = new flexRadioButton();
            }

            Forms_QLHS.intSoForms = 0;
        }
        //Lấy số từ chuổi tag cuả form
        private int GetIdFormInt(string strTag)
        {
            string strValue = Convert.ToString(strTag);
            char[] delimiters = new char[] { ' ','G','C','A', 'D', 'T', 'S', 'H', 'K', 'S', 'M', 'F', 'P','L','N','U','V', '_' };
            string[] strCode = strValue.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);
            return Convert.ToInt32(strCode[0]);
            
        }
        //Cắt bỏ khoảng trắng trong chuổi
        private string StringRevoveSpace(string strTag)
        {
            string strValue = Convert.ToString(strTag);
            char[] delimiters = new char[] { ' '};
            string[] strCode = strValue.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);
            return Convert.ToString(strCode[0]);

        }
        //Khởi tạo form
        public void InitForms(ref Form frmForm,ADODB.Connection Conn)
        {
            try
            {
                string strValue = Convert.ToString(frmForm.Tag);
                int intIDForm = GetIdFormInt(Convert.ToString(frmForm.Tag));
                string strSQL = "select ID_CODE,FORM_TABLE,DATA from System_Forms where CODE = " + "'" + strValue + "'";
                ADODB.Recordset Rs = new ADODB.Recordset();
                Rs.Open(strSQL, Conn, ADODB.CursorTypeEnum.adOpenStatic, ADODB.LockTypeEnum.adLockOptimistic, -1);
                if (Rs.RecordCount > 0)
                {
                    strSQL = "Select Proc_ListView,Proc_SaveData,Proc_tags from System_Childs where CODE = " + "'" + StringRevoveSpace(Rs.Fields[0].Value) + "'";
                }
                Forms_QLHS.fcForms[intIDForm].strFormTable = Rs.Fields[1].Value;
                Forms_QLHS.fcForms[intIDForm].strTitle = Rs.Fields[2].Value;

                Rs.Close();

                Rs.Open(strSQL, Conn, ADODB.CursorTypeEnum.adOpenStatic, ADODB.LockTypeEnum.adLockOptimistic, -1);
                if (Rs.RecordCount > 0)
                {
                    Forms_QLHS.fcForms[intIDForm].strProc_ListView = Rs.Fields[0].Value;
                    
                        for(int i=0;i< Forms_QLHS.fcForms[intIDForm].intSoListViewInForm;i++)
                        {
                            if (Forms_QLHS.fcListView[Forms_QLHS.fcForms[intIDForm].intListViewId[i]].fcListView.strProc == "")
                            {
                        Forms_QLHS.fcListView[Forms_QLHS.fcForms[intIDForm].intListViewId[i]].fcListView.strProc = Forms_QLHS.fcForms[intIDForm].strProc_ListView;
                        }
                        
                    }
                    
                    
                        for (int i = 0; i < Forms_QLHS.fcForms[intIDForm].intSoTextBoxInForm; i++)
                        {
                            if (Forms_QLHS.fcTextBox[Forms_QLHS.fcForms[intIDForm].intTextBoxID[i]].fcTextBox.strProc_ListView=="" && Forms_QLHS.fcTextBox[Forms_QLHS.fcForms[intIDForm].intTextBoxID[i]].fcTextBox.booGenCode==true)
                            Forms_QLHS.fcTextBox[Forms_QLHS.fcForms[intIDForm].intTextBoxID[i]].fcTextBox.strProc_ListView = Forms_QLHS.fcForms[intIDForm].strProc_ListView;
                        }
                    
                    Forms_QLHS.fcForms[intIDForm].strProc_SaveData = Rs.Fields[1].Value;
                    Forms_QLHS.fcForms[intIDForm].strProc_tags = Rs.Fields[2].Value;
                    
                }
                if (Forms_QLHS.fcForms[intIDForm].booActive == false)
                {
                    Forms_QLHS.fcForms[intIDForm].fcForm = frmForm;
                    Forms_QLHS.fcForms[intIDForm].fcForm.FormBorderStyle = FormBorderStyle.FixedSingle;
                    Forms_QLHS.fcForms[intIDForm].fcForm.Text = Forms_QLHS.fcForms[intIDForm].strTitle;
                    Forms_QLHS.fcForms[intIDForm].booActive = true;
                    Forms_QLHS.fcForms[intIDForm].strProc_ListView = strValue;
                    Forms_QLHS.fcForms[intIDForm].fcForm.MaximizeBox = false;
                    Forms_QLHS.fcForms[intIDForm].fcForm.KeyPreview = true;
                    Forms_QLHS.fcForms[intIDForm].fcForm.ControlBox=false;
                    Forms_QLHS.intSoForms++;
                }
                Rs.Close();

            }
            catch(Exception ex)
            {
                MessageBox.Show("Khởi tạo form" + Convert.ToString(ex));
            }
        }

        //Khởi tạo listview
        public void InitForms(string strTag,ref fcListView lstView)
        {
            string strValue = Convert.ToString(strTag);
            char[] delimiters = new char[] { ' ', 'S', 'M', 'F', '_' };
            string[] strCode = strValue.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);
            int intIDForm = GetIdFormInt(strTag);
            Forms_QLHS.fcListView[Forms_QLHS.intIdListView].fcListView = lstView;
            Forms_QLHS.fcListView[Forms_QLHS.intIdListView].fcListView.Conn = fcConn;
            Forms_QLHS.fcForms[intIDForm].intListViewId[Forms_QLHS.fcForms[intIDForm].intSoListViewInForm] = Forms_QLHS.intIdListView;
            Forms_QLHS.fcForms[intIDForm].intSoListViewInForm++;
            Forms_QLHS.intIdListView++;

        }
        //KHởi tạo button
        public void InitForms(string strTag, ref fcButton fcBut)
        {
            try
            {
                int intIDForm = GetIdFormInt(strTag);
                Forms_QLHS.fcButton[Forms_QLHS.intIDButton].fcButton = fcBut;
                Forms_QLHS.fcForms[intIDForm].intButtonID[Forms_QLHS.fcForms[intIDForm].intSoButtonInForm] = Forms_QLHS.intIDButton;
                Forms_QLHS.fcForms[intIDForm].intSoButtonInForm++;
                Forms_QLHS.intIDButton++;
            }
            catch (Exception ex)
            {
                MessageBox.Show("Init Form Button" + Convert.ToString(ex));
            }
        }
        //Khởi tạo textbox
        public void InitForms(string strTag, ref fcTextBox textbox)
        {
            try
            {
                int intIDForm = GetIdFormInt(strTag);
                Forms_QLHS.fcTextBox[Forms_QLHS.intIDTextBox].fcTextBox = textbox;
                
                Forms_QLHS.fcForms[intIDForm].intTextBoxID[Forms_QLHS.fcForms[intIDForm].intSoTextBoxInForm] = Forms_QLHS.intIDTextBox;
                if (textbox.fcGenCode == true || textbox.fcReadOnly==true)
                {
                    Forms_QLHS.fcTextBox[Forms_QLHS.intIDTextBox].fcTextBox.txtConn = fcConn;
                }
                if (textbox.fcDate == true)
                {
                    Forms_QLHS.fcTextBox[Forms_QLHS.intIDTextBox].fcTextBox.fcText = System.DateTime.Now.ToString("dd/MM/yyyy");
                }
                Forms_QLHS.fcForms[intIDForm].intSoTextBoxInForm++;
                Forms_QLHS.intIDTextBox++;
                
            }
            catch (Exception ex)
            {
                MessageBox.Show("Khởi tạo textbox \n" + Convert.ToString(ex));
            }
        }
        //Khởi tạo radio button
        public void InitForms(string strTag, ref RadioButton radio)
        {
            try
            {
                int intIDForm = GetIdFormInt(strTag);
                Forms_QLHS.fcRadio[Forms_QLHS.intIDRadioButton].fcRadio = radio;
                Forms_QLHS.fcRadio[Forms_QLHS.intIDRadioButton].strTag = Convert.ToString(radio.Tag);
                Forms_QLHS.fcForms[intIDForm].intRadioID[Forms_QLHS.fcForms[intIDForm].intSoRadioInForm] = Forms_QLHS.intIDRadioButton;
                Forms_QLHS.fcForms[intIDForm].intSoRadioInForm++;
                Forms_QLHS.intIDRadioButton++;
           }
            catch (Exception ex)
            {
                MessageBox.Show("Khởi tạo radio \n" + Convert.ToString(ex));
            }
        }
        //Sự kiện fcButton click
        public void fcButton_Click(string strIDForm,ref fcButton button,ADODB.Connection Conn)
        {
            try 
            {
                int intIDForm = GetIdFormInt(strIDForm);
                switch (button.intState)
                {
                    case STATE_THOAT:
                        Forms_QLHS.fcForms[intIDForm].fcForm.Visible = false;
                        break;

                    case STATE_ADD:

                        if (UpdateDataSQL(intIDForm, button.intState, Conn) == true)
                        {
                            button.intState = STATE_ADD;
                            ClearTextControll(intIDForm);
                            LoadListView(intIDForm);
                            GenerateCode(intIDForm);
                        }
                            break;
                    case STATE_DELETE:
                            UpdateDataSQL(intIDForm, button.intState, Conn);
                        
                        ClearTextControll(intIDForm);
                        LoadListView(intIDForm);
                            break;
                    case STATE_UPDATE:
                            if (UpdateDataSQL(intIDForm, button.intState, Conn) == true)
                            {
                                ClearTextControll(intIDForm);
                                LoadListView(intIDForm);
                                button.ChangeState(STATE_ADD);
                            }
                            else
                                button.ChangeState(STATE_UPDATE);
                        break;
                    case STATE_CLEAR:

                        GenerateCode(intIDForm);
                        ClearTextControll(intIDForm);
                                break;

                }
            }
                
           catch (Exception ex)
            {
                MessageBox.Show("fcButton click " + Convert.ToString(ex));
            }
        }
        private bool UpdateDataSQL(int intIDForm, int intState,ADODB.Connection Conn)
        {
            try
            {
                string strSQL = "set dateformat dmy ";
                strSQL += Forms_QLHS.fcForms[intIDForm].strProc_SaveData;
                strSQL += " " + intState;
                string strTag = Forms_QLHS.fcForms[intIDForm].strProc_tags;
                char[] delimiters = new char[] { '|' };
                string[] strFields = strTag.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);
                bool booCheckValidate = false;
                for (int i = 0; i < strFields.Length && booCheckValidate==false; i++)
                {

                    for (int j = 0; j < Forms_QLHS.fcForms[intIDForm].intSoTextBoxInForm && booCheckValidate == false; j++)
                    {
                        if (Forms_QLHS.fcTextBox[Forms_QLHS.fcForms[intIDForm].intTextBoxID[j]].fcTextBox.booGetIt == true && Forms_QLHS.fcTextBox[Forms_QLHS.fcForms[intIDForm].intTextBoxID[j]].fcTextBox.fcValidate == true && Forms_QLHS.fcTextBox[Forms_QLHS.fcForms[intIDForm].intTextBoxID[j]].fcTextBox.fcText == "" && intState!=3)
                        {
                            booCheckValidate = true;
                            MessageBox.Show("Hãy nhập đủ thông tin");
                            return false;
                        }
                        string strTemp = Forms_QLHS.fcTextBox[Forms_QLHS.fcForms[intIDForm].intTextBoxID[j]].fcTextBox.strFieldname;
                        if (strFields[i].ToUpper() == strTemp.ToUpper() && Forms_QLHS.fcTextBox[Forms_QLHS.fcForms[intIDForm].intTextBoxID[j]].fcTextBox.booGetIt == true )
                        {
                            if (Forms_QLHS.fcTextBox[Forms_QLHS.fcForms[intIDForm].intTextBoxID[j]].fcTextBox.fcParentTable == "")
                            {
                                if (Forms_QLHS.fcTextBox[Forms_QLHS.fcForms[intIDForm].intTextBoxID[j]].fcTextBox.fcDataType==fcTextBox.DataType.Text)
                                {
                                strSQL += ",N'" + Forms_QLHS.fcTextBox[Forms_QLHS.fcForms[intIDForm].intTextBoxID[j]].fcTextBox.fcText + "'";
                                    
                                }
                                else if(Forms_QLHS.fcTextBox[Forms_QLHS.fcForms[intIDForm].intTextBoxID[j]].fcTextBox.fcDataType==fcTextBox.DataType.Date)
                                {
                                    strSQL += ",'"+  Forms_QLHS.fcTextBox[Forms_QLHS.fcForms[intIDForm].intTextBoxID[j]].fcTextBox.fcText +"'";
                                }
                                else if (Forms_QLHS.fcTextBox[Forms_QLHS.fcForms[intIDForm].intTextBoxID[j]].fcTextBox.fcDataType == fcTextBox.DataType.Numberic)
                                {
                                    strSQL +=","+ Convert.ToDouble( Forms_QLHS.fcTextBox[Forms_QLHS.fcForms[intIDForm].intTextBoxID[j]].fcTextBox.fcText );
                                }
                            }
                            else
                            {
                                if (Forms_QLHS.fcTextBox[Forms_QLHS.fcForms[intIDForm].intTextBoxID[j]].fcTextBox.fcText != "")
                                    strSQL += ",N'" + Forms_QLHS.fcTextBox[Forms_QLHS.fcForms[intIDForm].intTextBoxID[j]].fcTextBox.fcCode + "'";
                                else
                                    strSQL += ",N''";
                            }
                        }
                    }
                    for (int j = 0; j < Forms_QLHS.fcForms[intIDForm].intSoRadioInForm && booCheckValidate == false; j++)
                    {

                        string strTemp = Forms_QLHS.fcRadio[Forms_QLHS.fcForms[intIDForm].intRadioID[j]].strTag;
                        if (strFields[i].ToUpper() == strTemp.ToUpper())
                        {
                            if (Forms_QLHS.fcRadio[Forms_QLHS.fcForms[intIDForm].intRadioID[j]].fcRadio.Checked == true)
                                strSQL += "," + "True";
                            else
                                strSQL += "," + "False";
                        }
                    }
                }
                bool result=false;
                if (booCheckValidate == false || intState == STATE_DELETE)
                {
                    ADODB.Recordset Rs = new ADODB.Recordset();


                    Rs.Open(strSQL, Conn, ADODB.CursorTypeEnum.adOpenStatic, ADODB.LockTypeEnum.adLockOptimistic, -1);
                    result = true;
                }
                
                return result;
            }
            catch (Exception ex)
            {
                MessageBox.Show("Update data SQL" + Convert.ToString(ex));
                return false;
            }
        }
        public void ClearTextControll(int intIDForm)
        {
            try 
            { 
                for (int i = 0; i < Forms_QLHS.fcForms[intIDForm].intSoTextBoxInForm; i++)
                {
                    string strTemp = " ";
                    Forms_QLHS.fcTextBox[Forms_QLHS.fcForms[intIDForm].intTextBoxID[i]].fcTextBox.fcText = strTemp;
                                 
                }
                for (int i = 0; i < Forms_QLHS.fcForms[intIDForm].intSoRadioInForm; i++)
                {

                    Forms_QLHS.fcRadio[Forms_QLHS.fcForms[intIDForm].intRadioID[i]].fcRadio.Checked = true;

                }
                for (int i = 0; i < Forms_QLHS.fcForms[intIDForm].intSoButtonInForm; i++)
                {
                    Forms_QLHS.fcButton[Forms_QLHS.fcForms[intIDForm].intButtonID[i]].fcButton.ChangeState( flexControls.STATE_ADD);

                }
                GenerateCode(intIDForm);
            }
            catch(Exception ex  )
            {
                MessageBox.Show("Clear text controll" + Convert.ToString(ex));
            }
        }
        public void ExcuteSQL(string strSQL,ADODB.Connection Conn)
        {
            try
            {
                ADODB.Recordset Rs = new ADODB.Recordset();
               Rs.Open(strSQL, Conn, ADODB.CursorTypeEnum.adOpenStatic, ADODB.LockTypeEnum.adLockOptimistic, -1);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Excute SQL \n" + ex.ToString());
            }
        }
        //Gọi form khi select treeview
        public string CallForms(int intIDForm)
        {
            if (Forms_QLHS.fcForms[intIDForm].booActive == true)
            {
                //Forms_QLHS.fcForms[intIDForm].fcForm.Load;
                LoadListView(intIDForm);
                GenerateCode(intIDForm);
                ClearTextControll(intIDForm);
                //Forms_QLHS.fcForms[intIDForm].fcForm.Visible=true;
                
                Forms_QLHS.fcForms[intIDForm].fcForm.Show();
                return Forms_QLHS.fcForms[intIDForm].fcForm.Text;
            }
            return "";
        }
        //Tạo code mới
        public void GenerateCode(int intIDForm)
        {
            try
            {
                int intSoTB = Forms_QLHS.fcForms[intIDForm].intSoTextBoxInForm;
                for (int i = 0; i < intSoTB; i++)
                {
                    if (Forms_QLHS.fcTextBox[Forms_QLHS.fcForms[intIDForm].intTextBoxID[i]].fcTextBox.fcGenCode == true)
                    {
                        string strSQL = Forms_QLHS.fcTextBox[Forms_QLHS.fcForms[intIDForm].intTextBoxID[i]].fcTextBox.fcProc_ListView;
                        if (strSQL != "")
                        {
                            ADODB.Recordset Rs =new ADODB.Recordset();
                            Rs.Open(strSQL, fcConn, ADODB.CursorTypeEnum.adOpenStatic, ADODB.LockTypeEnum.adLockReadOnly, -1);
                            string strCode;
                            int intNumber = 1;
                            if(Rs.RecordCount>0)
                            {
                                
                                
                                for (int j = 1; j <= Rs.RecordCount; j++)
                                {
                                    if (intNumber == GetIdFormInt(Rs.Fields[0].Value)) 
                                    {
                                        intNumber++;
                                        Rs.MoveFirst();
                                        j = 0;
                                    }
                                    else
                                    {
                                    Rs.MoveNext();
                                    }
                                }

                                
                            }
                            if (intNumber > 9)
                            {
                                strCode = Forms_QLHS.fcTextBox[Forms_QLHS.fcForms[intIDForm].intTextBoxID[i]].fcTextBox.fcCode + Convert.ToString(intNumber);
                            }
                            else
                            {
                                strCode = Forms_QLHS.fcTextBox[Forms_QLHS.fcForms[intIDForm].intTextBoxID[i]].fcTextBox.fcCode + "0" + Convert.ToString(intNumber);
                            }

                            Forms_QLHS.fcTextBox[Forms_QLHS.fcForms[intIDForm].intTextBoxID[i]].fcTextBox.fcText = strCode;
                            Rs.Close();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Tạo code mới" + Convert.ToString(ex));
            }
        }
        //Load listview default cho form
        public void LoadListView(int intIDForm)
        {
            int intIDListView=0;
            int intSoListView=0;
            intSoListView = Forms_QLHS.fcForms[intIDForm].intSoListViewInForm;
            for (int i = 0; i < intSoListView; i++)
            {
                
                intIDListView=Forms_QLHS.fcForms[intIDForm].intListViewId[i];
                string strSQL=Convert.ToString(Forms_QLHS.fcListView[intIDListView].fcListView.strProc);
                if (strSQL == "" )
                {
                    strSQL ="exec " + Forms_QLHS.fcForms[intIDForm].strProc_ListView;
                    
                }
                //if (Forms_QLHS.fcListView[intIDListView].fcListView.fcProc != "" && Forms_QLHS.fcListView[intIDListView].fcListView.booProcFromData == true)
                if ( Forms_QLHS.fcListView[intIDListView].fcListView.booProcFromData == true)
                {
                    if (Forms_QLHS.fcListView[intIDListView].fcListView.fcTextBox==true)
                        Forms_QLHS.fcListView[intIDListView].fcListView.LoadDataFromTextBox();
                    else
                    Forms_QLHS.fcListView[intIDListView].fcListView.LoadData(strSQL,fcConn);
                }
            }
        }
        //Hàm kết nổi server
        public void DatabaseConnection(bool booCallBy)
        {
            string[] strXML = new string[10];
            if (!ReadXML(XMLFILENAME, ref strXML) || booCallBy==true)
            {
                fcConnect.ShowDialog();
                fcConn = fcConnect.conn;
            }
            else
            {
                 strServer = strXML[flexControls.ELE_SERVER];
                 strUser = strXML[flexControls.ELE_USER];
                 strPassword = strXML[flexControls.ELE_PASSWORD];
                 strDatabase = strXML[flexControls.ELE_DATABASE];
                if (strDatabase != "" && strDatabase != null)
                {
                    if (!ConnectDatabase(false))
                    {
                        fcConnect.ShowDialog();
                        fcConn = fcConnect.conn;
                    }
                }
            }
        }
        //Hàm kết nôí database
        public bool ConnectDatabase(bool NonUser)
        {
            try
            {
                string strString="Data Source="+strServer+";Initial Catalog="+strDatabase+";Persist Security Info=True;User ID="+strUser+";Password="+ strPassword ;
                if (NonUser == true)
                    strString = "Data Source=DUONGKYHIEU-PC;Initial Catalog=QLAU;Integrated Security=True";
                
                fcConn = new ADODB.Connection();
                fcConn.ConnectionString = "Provider=SQLOLEDB.1;Password=" + strPassword + ";Persist Security Info=True;User ID=" + strUser + ";Initial Catalog=" + strDatabase + "  ;Data Source=" + strServer + "";
                fcConn.Open(fcConn.ConnectionString, strUser, strPassword, -1);

                if (fcConn.State == 1)
                {
                    fcSQLConn=new SqlConnection(strString);
                    fcSQLConn.Open();
                    return true;

                }
                return false;
            }
            catch (Exception ex)
            {
                ex.Message.ToString();
                return false;
            }
        }
        //Đọc file XML
        public bool ReadXML(string strXMLName, ref string[] strXML)
        {
            try
            {
                strSQL =  new string[MAXELEMENT];
                string strpath = Application.StartupPath;
                XmlDataDocument reader = new XmlDataDocument();
                XmlNodeList xmlnode;
                FileStream fs = new FileStream(Application.StartupPath + "\\Xml\\" + "DatabaseInfo.xml", FileMode.Open, FileAccess.Read);
                reader.Load(fs);
                fs.Flush();
                fs.Close();
                xmlnode = reader.GetElementsByTagName("information");
                for (int i = 0; i < xmlnode[0].ChildNodes.Count; i++)
                {
                    switch (xmlnode[0].ChildNodes.Item(i).Name)
                    {
                        case "Server":
                            strSQL[ELE_SERVER] = Convert.ToString(xmlnode[0].ChildNodes.Item(i).InnerText.Trim());
                            if (strSQL[ELE_SERVER] == "" || strSQL[ELE_SERVER]==null)
                                return false;
                            break;
                        case "User":
                            strSQL[ELE_USER] = Convert.ToString(xmlnode[0].ChildNodes.Item(i).InnerText.Trim());
                            if (strSQL[ELE_USER] == "" || strSQL[ELE_SERVER] == null)
                                return false;
                            break;
                        case "Password":
                            strSQL[ELE_PASSWORD] = Convert.ToString(xmlnode[0].ChildNodes.Item(i).InnerText.Trim());
                            if (strSQL[ELE_PASSWORD] == "" || strSQL[ELE_SERVER] == null)
                                return false;
                            break;
                        case "Database":
                            strSQL[ELE_DATABASE] = Convert.ToString(xmlnode[0].ChildNodes.Item(i).InnerText.Trim());
                            if (strSQL[ELE_DATABASE] == "" || strSQL[ELE_SERVER] == null)
                                return false;
                            break;
                    }

                }
                strXML = strSQL;
                return true;
            }
            catch (Exception ex)
            {
                ex.Message.ToString();
                return false;
            }
        }
        public void AddDataToControll(string strCode,string strTag,ADODB.Connection Conn)
        {
            try
            {
                int intIdForm = GetIdFormInt(strTag);
                string strSQL = "select * from " + Forms_QLHS.fcForms[intIdForm].strFormTable + " where Code =" + "'" + strCode+ "'";
                ADODB.Recordset Rs = new ADODB.Recordset();
                Rs.Open(strSQL, Conn, ADODB.CursorTypeEnum.adOpenStatic, ADODB.LockTypeEnum.adLockOptimistic, -1);
                if (Rs.RecordCount > 0)
                {
                    for(int i=0;i< Rs.Fields.Count;i++)
                    {
                        for (int j = 0; j < Forms_QLHS.fcForms[intIdForm].intSoTextBoxInForm; j++)
                        {
                            if (Forms_QLHS.fcTextBox[Forms_QLHS.fcForms[intIdForm].intTextBoxID[j]].fcTextBox.strFieldname.ToUpper() == Rs.Fields[i].Name.ToUpper())
                            {

                                if (Forms_QLHS.fcTextBox[Forms_QLHS.fcForms[intIdForm].intTextBoxID[j]].fcTextBox.strParent != "")
                                    {
                                        string strGetField = Forms_QLHS.fcTextBox[Forms_QLHS.fcForms[intIdForm].intTextBoxID[j]].fcTextBox.strParent;
                                        char[] delimiters = new char[] {  '|' };
                                        string[] strParent=strGetField.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);
                                        //for (int k=0;k<strParent.Length;k++)
                                        //{
                                      //      strGetField = strParent[k];
                                        //}
                                            strGetField = "DATA";
                                            if (strParent.Length > 1)
                                            {
                                                strGetField = strParent[1];
                                            }
                                            
                                        strSQL = "Select "+ strGetField + " from " + strParent[0] + " where CODE = '" + Rs.Fields[i].Value + "'";
                                        ADODB.Recordset RsChild = new ADODB.Recordset();
                                        RsChild.Open(strSQL, Conn, ADODB.CursorTypeEnum.adOpenStatic, ADODB.LockTypeEnum.adLockOptimistic, -1);
                                        if (RsChild.RecordCount > 0)
                                        {
                                            Forms_QLHS.fcTextBox[Forms_QLHS.fcForms[intIdForm].intTextBoxID[j]].fcTextBox.fcText = Convert.ToString(RsChild.Fields[0].Value);
                                            Forms_QLHS.fcTextBox[Forms_QLHS.fcForms[intIdForm].intTextBoxID[j]].fcTextBox.strCode = Convert.ToString(Rs.Fields[i].Value);
                                        }
                                        RsChild.Close();
                                    }
                                    else
                                    {
                                        Forms_QLHS.fcTextBox[Forms_QLHS.fcForms[intIdForm].intTextBoxID[j]].fcTextBox.fcText =Convert.ToString( Rs.Fields[i].Value);
                                    }
                               
                            }
                        }
                        for (int j = 0; j < Forms_QLHS.fcForms[intIdForm].intSoRadioInForm; j++)
                        {
                            if (Forms_QLHS.fcRadio[Forms_QLHS.fcForms[intIdForm].intRadioID[j]].strTag.ToUpper() == Rs.Fields[i].Name.ToUpper())
                            {
                                string temp = Convert.ToString(Rs.Fields[i].Value);
                                if (temp== "True")
                                    Forms_QLHS.fcRadio[Forms_QLHS.fcForms[intIdForm].intRadioID[j]].fcRadio.Checked = true;
                                else
                                    Forms_QLHS.fcRadio[Forms_QLHS.fcForms[intIdForm].intRadioID[j]].fcRadio.Checked = false;
                            }
                        }
                        
                    }
                }
                Rs.Close();
            }
            catch(Exception ex)
            {
                MessageBox.Show("Add data to controlls" + Convert.ToString(ex));
            }
        }

    }
}

